//
//  DataBaseHandler.m
//  18--Homework
//
//  Created by 冀永金 on 16/8/8.
//  Copyright © 2016年 冀永金. All rights reserved.
//

#import "DataBaseHandler.h"
#import "Contact.h"
@implementation DataBaseHandler
+(DataBaseHandler *)shareManager {
    static DataBaseHandler *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[DataBaseHandler alloc]init];
    });
    return manager;
}

-(void)openTable {
    NSString *dbPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject] stringByAppendingPathComponent:@"contact.db"];
    NSLog(@"%@",NSHomeDirectory());
    int result = sqlite3_open(dbPath.UTF8String, &dbPoint);
    [self isOperationResult:result alertText:@"打开数据库"];
    
    
}
- (void)createTable {
    NSString *createSQL = @"create table if not exists contact(number integer primary key autoincrement,name text,phoneNumber text)";
    int result = sqlite3_exec(dbPoint, createSQL.UTF8String, NULL, NULL, NULL);
    [self isOperationResult:result alertText:@"创建表格"];
}
- (void)insertContact:(Contact *)contact {
    NSString *insertSQL = [NSString stringWithFormat:@"insert into contact (name,phoneNumber) values('%@','%@')",contact.name,contact.phoneNumber];
    int result = sqlite3_exec(dbPoint, insertSQL.UTF8String, NULL, NULL, NULL);
    [self isOperationResult:result alertText:@"插入数据"];
    
}
-(void)deleteContact:(NSInteger)number {
    NSString *deleteSQL = [NSString stringWithFormat:@"delete from student where number = '%ld'",number];
    int result = sqlite3_exec(dbPoint, deleteSQL.UTF8String, NULL, NULL, NULL);
    [self isOperationResult:result alertText:@"删除数据"];
}

- (NSArray *)selectAll {
    NSMutableArray *marr = [NSMutableArray array];
    NSString *selectAllSQL = @"select * from contact";
    sqlite3_stmt *stmt = nil;
    int result = sqlite3_prepare(dbPoint, selectAllSQL.UTF8String, -1, &stmt, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"666");
        while (sqlite3_step(stmt)  == SQLITE_ROW) {
            const unsigned char *name = sqlite3_column_text(stmt, 1);
            const unsigned char *phoneNumber = sqlite3_column_text(stmt, 2);
            Contact *contact = [[Contact alloc]init];
            contact.name = [NSString stringWithUTF8String:(const char *)name];
            contact.phoneNumber = [NSString stringWithUTF8String:(const char *)phoneNumber];
            [marr addObject:contact];
        }
        sqlite3_finalize(stmt);
    }
    return marr;
}
- (NSMutableArray *)selectForName:(NSString *)name {
    NSMutableArray *marr = [NSMutableArray array];
    
    NSString *select =[NSString stringWithFormat:@"SELECT * FROM contact WHERE name = '%@' ",name];
    sqlite3_stmt *stmt = nil;
    int result = sqlite3_prepare_v2(dbPoint, select.UTF8String, -1, &stmt, nil);
    if (result == SQLITE_OK) {
        NSLog(@"准备就绪");
        
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            
           ;
            
            const unsigned char *phoneNumber = sqlite3_column_text(stmt, 2);
            Contact *contct = [[Contact alloc]init];
            contct.name = name;
            contct.phoneNumber = [NSString stringWithUTF8String:(const char *)phoneNumber];
        [marr addObject:contct];
            
        }
        sqlite3_finalize(stmt);
        
    }else {
        NSLog(@"不能正常查询 code:%d",result);
        
        //销毁
        sqlite3_finalize(stmt);
    }
    
    return  marr;
}

- (void)isOperationResult:(int)result alertText:(NSString *)text {
    if (result == SQLITE_OK) {
        NSLog(@"%@成功",text);
    }else {
        NSLog(@"%@失败---code:%d",text,result);
    }
}
@end
